<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
*/


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport('joomla.application.component.model');


class GalleryModelImage extends JModel
{
	function __construct()
	{
		parent::__construct();

		$cid = JRequest::getVar('cid',  array(), '', 'array');
		if (count($cid) > 0 && $cid[0] > 0)
			$this->setId((int)$cid[0]);
	}

	function setId($id)
	{
		$this->_id = (int)$id;
		$this->_data = null;
	}

	function getData()
	{
		if ($this->_loadData())
		{
			// Check to see if the category is published.
			if (!$this->_data->cat_pub)
			{
				JError::raiseError(404, JText::_("Resource Not Found"));
				return;
			}

			// Check whether category access level allows access
			$user = &JFactory::getUser();
			if ($this->_data->cat_access > $user->get('aid', 0))
			{
				JError::raiseError(403, JText::_('ALERTNOTAUTH'));
				return;
			}
		}
		else
			$this->_initData();

		return $this->_data;
	}

	function isCheckedOut($uid = null)
	{
		if ($this->_loadData())
		{
			if (null !== $uid)
				return $this->_data->checked_out && $this->_data->checked_out != $uid;
			else
				return $this->_data->checked_out;
		}
	}

	function checkin()
	{
		if ($this->_id)
		{
			$row = $this->getTable();
			if(!$row->checkin($this->_id))
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			return true;
		}
		return true;
	}

	function checkout($uid = null)
	{
		if ($this->_id)
		{
			// Use the current user's id if one wasn't provided.
			if (null !== $uid)
			{
				$user   = JFactory::getUser();
				$uid    = $user->get('id');
			}

			$row = $this->getTable();
			if(!$row->checkout($uid, $this->_id))
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			return true;
		}
		return false;
	}

	function store($data)
	{
		$row = $this->getTable();

		// Bind the form fields to the table.
		if (!$row->bind($data))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Create the created on timestamp.
		$row->created_on = gmdate('Y-m-d H:i:s');

		// if it is a new item, order last in appropriate group.
		if (!$row->id)
		{
			$where = 'catid = ' . (int)$row->catid;
			$row->ordering = $row->getNextOrder($where);
		}

		// Make sure the data in the row is valid.
		if (!$row->check())
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store())
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}

	function delete($cid = array())
	{
		if (count($cid))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode(',', $cid);
			$query = 'DELETE FROM #__weblinks'
				. "\n	WHERE id IN ($cids)"
				;
			$this->_db->setQuery($query);
			if(!$this->_db->query())
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

	function publish($cid = array(), $publish = 1)
	{
		$user = JFactory::getUser();

		if (count($cid))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode(',', $cid);
			$publish = (int)$publish;
			$uid = $user->get('id');

			$query = 'UPDATE #__weblinks'
				. "\n	SET published = $publish"
				. "\n	WHERE id IN ($cids)"
				. '\n		AND (checked_out = 0 OR checked_out = $uid)'
				;
			$this->_db->setQuery($query);
			if (!$this->_db->query())
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

	function move($direction)
	{
		$row = $this->getTable();
		if (!$row->load($this->_id))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		$where = ' catid = ' . (int)$row->catid . ' AND published >= 0 ';
		if (!$row->move($direction, $where))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}

	function saveOrder($cid = array(), $order)
	{
		$row = $this->getTable();
		$groupings = array();

		// Update ordering values.
		JArrayHelper::toInteger($cid);
		for ($i=0; $i < count($cid); $i++)
		{
			$row->load($cid[$i]);

			// Track categories
			$groupings[] = $row->catid;

			if ($row->ordering != $order[$i])
			{
				$row->ordering = $order[$i];
				if (!$row->store())
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}

		// Execute the reorder() method for each parent group.
		$groupings = array_unique($groupings);
		foreach ($groupings as $group)
			$row->reorder('catid = ' . $group);

		return true;
	}

	function _loadData()
	{
		// Lets load the content if it doesn't already exist.
		if (null === $this->_data)
		{
			$query = 'SELECT i.*'
				. "\n		 , c.title AS category"
				. "\n		 , c.published AS cat_pub"
				. "\n		 , c.access AS cat_access"
				. "\n	FROM #__gallery_images i"
				. "\n		LEFT JOIN #__categories c ON c.id = i.catid"
				. "\n	WHERE i.id = $this->_id"
				;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			if (null === $this->_data)
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}

	function _initData()
	{
		// Lets load the content if it doesn't already exist
		if (null !== $this->_data)
		{
			$data = new stdClass();
            $data->id               = 0;
            $data->sectionid        = 0;
            $data->catid            = 0;
            $data->title            = null;
            $data->alias            = null;
            $data->file_url         = null;
            $data->thumb_url        = null;
            $data->created_on       = null;
            $data->hits             = 0;
            $data->published        = 0;
            $data->checked_out      = 0;
            $data->checked_out_time = 0;
            $data->ordering         = 0;
            $data->category         = null;
            $this->_data            = $data;
		}
	}

	private $_data;

	private $_id;
}


?>